/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tpdisenio2009.mvp.presenter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import tpdisenio2009.Tpdisenio2009App;
import tpdisenio2009.model.beans.User;
import tpdisenio2009.mvp.display.QueryPlayerScoresView;
import tpdisenio2009.services.FantasyTeamPlayerService;
import tpdisenio2009.services.FantasyTeamService;
import tpdisenio2009.services.TournamentDateService;

/**
 *
 * @author Pablito
 */
public class QueryPlayerScoresPresenter  extends DefaultPresenter<QueryPlayerScoresView> {

    private final FantasyTeamPlayerService ftpService;

    private final FantasyTeamService ftService;

    private final TournamentDateService tdService;

    private ActionListener exitListener;

    private ItemListener dateChangeListener;

    private final User user;

    public QueryPlayerScoresPresenter( QueryPlayerScoresView view ) {
        super( view );

        ftpService = new FantasyTeamPlayerService();
        ftService = new FantasyTeamService();
        tdService = new TournamentDateService();
        user = (User) Tpdisenio2009App.loggedUser;

        getView().setFantasyTeamName( user.getFantasyTeam().getName() );
        getView().setCotization( ftService.getCotization( user ) );
        getView().setDates( tdService.getClosedTournamentDates() );
        getView().setDatePoints( ftService.getFantasyTeamPointsByDate( user.getFantasyTeam(), getView().getDate() ) );
        getView().setTournamentPoints( ftService.getFantasyTeamTotalPoints( user.getFantasyTeam() ) );
        getView().loadFantasyTeam( ftpService.getTeamWithScoreAndPosition( user.getFantasyTeam(), getView().getDate() ) );

        bind();
    }

    public void bind() {
        dateChangeListener = new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                getView().setCotization( ftService.getCotization( user ) );
                getView().setDatePoints( ftService.getFantasyTeamPointsByDate( user.getFantasyTeam(), getView().getDate() ) );
                getView().loadFantasyTeam( ftpService.getTeamWithScoreAndPosition( user.getFantasyTeam() , getView().getDate() ) );
            }
        };
        getView().addDateChangeListener( dateChangeListener );

        exitListener = new ActionListener() {
            public void actionPerformed( ActionEvent e ) {
                getView().close();
            }
        };
        getView().addExitListener( exitListener );
    }

    public void unbind() {
        if (dateChangeListener != null)
            getView().removeDateChangeListener( dateChangeListener );
        if (exitListener != null)
            getView().removeExitListener( exitListener );
    }

}
